#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../src/protocole.h"

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

     tcp.connectToHost("127.0.0.1",1929);


}

void MainWindow::on_pushButton_2_clicked()
{
    dbi_header h;
    h.type = DBI_WELCOME;
    h.len = SIZ_WELCOME + SIZ_HEADER;

    dbi_welcome w;
    w.client_ver = 0x01;
    strcpy(w.client_version_txt,"TestClient v0.2a rev 45");
    w.hash_user_name = 0;
    w.hash_user_pass = 0;
    w.my_time.milisecond = 0;
    w.my_time.time = time(0);
    w.read_only = 0;

    QByteArray ba;
    ba = QByteArray::fromRawData((char*)&h, sizeof(dbi_header));
    ba += QByteArray::fromRawData((char*)&w,SIZ_WELCOME);

    tcp.write(ba);

}

void MainWindow::on_pushButton_3_clicked()
{
    dbi_header h;
    h.type = DBI_MDATA;
    h.len = SIZ_MDATA + SIZ_HEADER;

    dbi_mdata mdata;
    mdata.channel = (1 << 9) | 16;      // dev 1, ch 16
    mdata.config_crc = 0;
    mdata.config_dev_num = 0;
    mdata.flags = 0;        // ok
    mdata.value = 432;

    QByteArray ba;
    ba = QByteArray::fromRawData((char*)&h, sizeof(dbi_header));
    ba += QByteArray::fromRawData((char*)&mdata,SIZ_MDATA);

    tcp.write(ba);
}
